package it.csi.finregio.finreges.client.validators;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.validator.DateValidator;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import it.crp.crparch.web.struts.validator.CRPStrutsValidator;
import it.csi.finregio.finreges.presentation.form.SchedaForm;

public class SalvaNoteScadenzaValidator implements CRPStrutsValidator {

	public ActionMessages validate(ActionMapping mapping,
			HttpServletRequest request, ActionForm form) {
		SchedaForm sf = (SchedaForm) form;
		ActionMessages am = new ActionMessages();
		if (sf.getTipoInserimento().equals("1")) {
			String data = sf.getDataScadenza();
			DateValidator dateValidator = DateValidator.getInstance();
			String datePattern = "dd/MM/yyyy";
			if (data != null && !data.trim().equals("")) {
				if (!dateValidator.isValid(data, datePattern, true)) {
					am.add("dataScadenza", new ActionMessage(
							"alert.date.format"));
				}
			}
		}
		return am;
	}

}
